package mc0101.mccache.util;

import java.io.UnsupportedEncodingException;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;

public class ReCommonUtils {
    public static String forcetrim(String arg){
        if (arg == null || "".equals(arg)){
            return "";
        }

        char[] value = arg.toCharArray();
        int len = value.length;
        int st = 0;

        while ((st < len) && (value[st] <= ' ' || value[st] == '　')) {
            st++;
        }
        while ((st < len) && (value[len - 1] <= ' ' || value[len - 1] == '　')) {
            len--;
        }
        return ((st > 0) || (len < value.length)) ? arg.substring(st, len) : arg;
    }

    // 去除字符串两边的空格,覆盖全角半角
    public static String oldForcetrim(String arg) {
        // arg = arg == null ? "" : arg.replaceAll("　", " ").trim();
        arg = arg == null ? "" : arg.trim();
        if (!"".equals(arg) && arg.trim().length() > 0) {
            arg = lTrim(arg);
            StringBuffer sb = new StringBuffer(arg);
            arg = sb.reverse().toString();
            arg = lTrim(arg);
            sb = new StringBuffer(arg);
            arg = sb.reverse().toString();

        }
        return arg.trim();
    }

    // 去除字符串左边的空格,覆盖全角半角
    public static String lTrim(String arg) {
        int index = arg.indexOf("　");
        String subs = arg.substring(0, index + 1);
        if (subs.length() > 0) {
            subs = subs.replaceAll("　", " ").trim();
            if (subs.length() == 0) {
                arg = arg.substring(index + 1);
                arg = lTrim(arg);
            }
        }
        return arg.trim();
    }

    public static void main(String[] args) {
        System.out.println(ReCommonUtils.forcetrim("   　　  "));
    }


    /**
     * 功能：判断一个字符串是否包含特殊字符
     *
     * @param arg
     *            要判断的字符串
     * @return true 提供的参数string不包含特殊字符
     * @return false 提供的参数string包含特殊字符
     */
    public static boolean isConSpeCharacters(String arg) {
        if (arg.replaceAll("[\u4e00-\u9fa5]*[a-z]*[A-Z]*\\d*-*_*\\s*", "").length() == 0) {
            return true;
        }
        return false;
    }

    // 数字长型判断，符合Long类型返回true,否则返回false
    public static boolean isLegalLong(String value) {
        boolean result = false;
        try {
            Long.parseLong(value.trim());
            result = true;
        } catch (Exception e) {
            result = false;
        }
        return result;
    }

    // 数字型判断，符合INTEGER类型返回true,否则返回false
    public static boolean isLegalInteger(String value) {
        boolean result = false;
        try {
            Integer.parseInt(value.trim());
            result = true;
        } catch (Exception e) {
            result = false;
        }
        return result;
    }

    // 半角判断,符合半角逻辑返回true，否则返回false
    public static boolean isLegalSemiAngle(String value) {
        boolean result = false;
        if (value == null) {
            result = true;
        }
        String str = value.trim();
        byte[] gbkBytes = null;
        try {
            gbkBytes = str.getBytes("GBK");
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
            gbkBytes = new byte[0];
        }
        if (gbkBytes.length == str.length()) {// 全是半角
            result = true;
        }
        return result;
    }

    // 全角判断,符合全角逻辑返回true，否则返回false
    public static boolean isLegalWholeAngle(String value) {
        boolean result = false;
        if (value == null) {
            result = true;
        }
//		String str = value.replaceAll("　", " ").trim();
        String str = value.trim();
        byte[] gbkBytes = null;
        try {
            gbkBytes = str.getBytes("GBK");
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
            gbkBytes = new byte[0];
        }
        if (gbkBytes.length == str.length() * 2) {// 全是全角
            result = true;
        }
        return result;
    }

    // 半角全角混用判断,符合混用逻辑返回true，否则返回false
    public static boolean isLegalAngle(String value) {
        boolean result = false;
        if (value == null) {
            result = true;
        }


        String str = value.trim();
        byte[] gbkBytes = null;
        try {
            gbkBytes = str.getBytes("GBK");
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
            gbkBytes = new byte[0];
        }
        if (gbkBytes.length > str.length() && gbkBytes.length < str.length() * 2) {// 混合
            result = true;
        }
        return result;
    }

    // 获取未来第dayCount天的日期
    public static Date getDaysAfter(Date date, int dayCount) {
        if (date == null)
            return null;
        Calendar calendar = new GregorianCalendar();
        calendar.setTime(date);
        calendar.add(Calendar.DATE, dayCount);
        return calendar.getTime();
    }
}